<template>
    <div>
        <div class="container">
            <div class="handle-box">
                集群名称:
                <el-input v-model="query.name" placeholder="集群名称" class="handle-input mr10"></el-input>IP地址:
                <el-input v-model="query.ipAddress" placeholder="IP地址" class="handle-input mr10"></el-input>状态:
                <el-select v-model="query.status" placeholder="全部" class="handle-select mr10">
                    <el-option key="1" label="启动" value="启动"></el-option>
                    <el-option key="2" label="停止" value="停止"></el-option>
                </el-select>
                <el-button type="primary" @click="handleSearch">查询</el-button>
                <el-button type="primary" @click="handleReset">重置</el-button>
                <el-button type="primary" @click="handleAdd">添加</el-button>
            </div>
            <el-table
                :data="tableData"
                border
                class="table"
                ref="multipleTable"
                header-cell-class-name="table-header"
                @selection-change="handleSelectionChange"
            >
                <el-table-column type="selection" width="55" align="center"></el-table-column>
                <el-table-column prop="name" label="集群名称" align="center"></el-table-column>
                <el-table-column prop="ipAddress" label="IP地址" align="center"></el-table-column>
                <el-table-column prop="port" label="端口号" align="center"></el-table-column>
                <el-table-column label="运行状态" align="center">
                    <template slot-scope="scope">
                        <el-tag
                            :type="scope.row.state==='启动'?'success':(scope.row.state==='停止'?'danger':'')"
                        >{{scope.row.state}}</el-tag>
                    </template>
                </el-table-column>

                <el-table-column label="操作" width="180" align="center">
                    <template slot-scope="scope">
                        <el-button
                            type="text"
                            class="pink"
                            @click="handleEdit(scope.$index, scope.row)"
                        >编辑</el-button>
                        <el-button type="text" class="pink" @click="toNext">监控</el-button>
                        <el-button
                            type="text"
                            class="pink"
                            @click="nodeDetails(scope.$index, scope.row)"
                        >节点详情</el-button>
                    </template>
                </el-table-column>
            </el-table>
            <div class="pagination">
                <el-pagination
                    background
                    layout="total, prev, pager, next"
                    :current-page="query.pageIndex"
                    :page-size="query.pageSize"
                    :total="pageTotal"
                    @current-change="handlePageChange"
                ></el-pagination>
            </div>
        </div>
    </div>
</template>

<script>
import { fetchData } from '../api/index';

export default {
    name: 'list',
    data() {
        return {
            query: {
                status: '',
                name: '',
                ipAddress: '',
                pageIndex: 1,
                pageSize: 10
            },
            tableData: [],
            multipleSelection: [],
            delList: [],
            editVisible: false,
            pageTotal: 0,
            form: {},
            idx: -1,
            id: -1
        };
    },
    //切换面包屑,获取模拟数据
    created() {
        this.$emit('change', this.$route.name);
        this.getData();
    },

    methods: {
        //跳转到大屏监控页面
        toNext() {
            this.$router.push({ path: '/monitoring', query: { plan: 'minor' } });
        },
        // 获取 easy-mock 的模拟数据
        getData() {
            fetchData(this.query).then((res) => {
                console.log(res);
                this.tableData = res.list;
                this.pageTotal = res.pageTotal || 50;
            });
        },
        // 触发查询按钮
        handleSearch() {
            this.$message('点击了查询按钮');
        },
        // 触发重置按钮
        handleReset() {
            this.$message('点击了重置按钮');
        },
        // 触发添加按钮
        handleAdd() {
            this.$router.push('/table/addNode');
        },
        // 多选操作
        handleSelectionChange(val) {
            this.multipleSelection = val;
        },
        //节点详情
        nodeDetails() {
            this.$router.push('/table/nodeDetail');
        },
        // 编辑操作
        handleEdit(index, row) {
            console.log(index, row);
            this.$router.push('/table/editNode');
        },
        // 分页导航
        handlePageChange(val) {
            this.$set(this.query, 'pageIndex', val);
            this.getData();
        }
    }
};
</script>

<style scoped>
.handle-box {
    margin-bottom: 20px;
}

.handle-select {
    width: 120px;
}

.handle-input {
    width: 200px;
    display: inline-block;
}
.table {
    width: 100%;
    font-size: 14px;
}
.pink {
    color: pink;
}
.mr10 {
    margin-right: 10px;
}
.table-td-thumb {
    display: block;
    margin: auto;
    width: 40px;
    height: 40px;
}
</style>
